'use client'; import { useForm, SubmitHandler } from 'react-hook-form'; import { yupResolver } from '@hookform/resolvers/yup'; import * as yup from 'yup'; import clsx from 'clsx'; import { AccountChangePasswordFormType } from '@akinon/next/types'; import { Button, Input, Link } from '@theme/components'; import { ROUTES } from 'routes'; import { useUpdatePasswordMutation } from '@akinon/next/data/client/account'; import { useMemo, useState } from 'react'; import { useSearchParams, usePathname } from 'next/navigation'; import { useRouter } from '@akinon/next/hooks'; import { useLocalization } from '@akinon/next/hooks'; import PasswordRulesFeedback from '@theme/components/password-rules-feedback'; import { Trans } from '@akinon/next/components/trans'; const accountChangePasswordSchema = (t) => yup.object().shape({ old_password: yup .string() .required(t('account.change_password.form.required')), new_password1: yup .string() .required(t('account.change_password.form.required')) .matches( /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z\d]).{6,}$/, t('account.change_password.form.error.password_rule') ) .max(50, t('account.change_password.form.error.password_max')), new_password2: yup .string() .oneOf( [yup.ref('new_password1')], t('account.change_password.form.new_password_repeat.error.not_same') ) .required(t('account.change_password.form.required')) }); export default function Page() { const { t } = useLocalization(); const { register, handleSubmit, watch, formState: { errors } } = useForm({ resolver: yupResolver(accountChangePasswordSchema(t)) }); const [formError, setFormError] = useState(null); const [updatePassword] = useUpdatePasswordMutation(); const router = useRouter(); const searchParams = useSearchParams(); const pathname = usePathname(); const passwordValue = watch('new_password1', ''); const onSubmit: SubmitHandler = (data) => { updatePassword(data) .unwrap() .then(() => { router.push(pathname + '?formSuccess=true'); }) .catch((error) => setFormError(error.data)); }; const LinkText = (props) => { return ( {props.title} ); }; const successParam = searchParams.get('formSuccess'); const isSuccess = useMemo(() => successParam === 'true', [successParam]); return (

{t('account.change_password.header.title')}

{isSuccess ? (

{t('account.change_password.form.success.title')}

{t('account.change_password.form.success.description')}

{t('account.change_password.form.success.button')}
) : (
{formError && (
{formError.old_password}
)}
)}

{t('account.change_password.info.title')}

), Faq: ( ) }} />

); }